/**
 * @brief NinePuzzle
 * A problem of TopCoder.
 *
 * @author XadillaX
 */
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <list>
#include <map>
#include <string.h>
#include <time.h>
#include <cstdlib>
#include <stack>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

#define INF 0x3f3f3f3f
#define clr(x) memset((x), 0, sizeof(x))
#define inf(x) memset((x), 0x7f, sizeof(x))
#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)
const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

class NinePuzzle {
public:
    int getMinimumCost(string init, string goal);
    void initial()
    {
    	adj[0].push_back(1); adj[0].push_back(2);
    	adj[1].push_back(0); adj[1].push_back(2); adj[1].push_back(3); adj[1].push_back(4);
    	adj[2].push_back(0); adj[2].push_back(1); adj[2].push_back(5); adj[2].push_back(4);
    	adj[3].push_back(1); adj[3].push_back(4); adj[3].push_back(6); adj[3].push_back(7);
    	adj[4].push_back(1); adj[4].push_back(2); adj[4].push_back(3); adj[4].push_back(5); adj[4].push_back(7); adj[4].push_back(8);
    	adj[5].push_back(2); adj[5].push_back(4); adj[5].push_back(8); adj[5].push_back(9);
    	adj[6].push_back(3); adj[6].push_back(7);
    	adj[7].push_back(3); adj[7].push_back(4); adj[7].push_back(8); adj[7].push_back(6);
    	adj[8].push_back(5); adj[8].push_back(4); adj[8].push_back(7); adj[8].push_back(9);
    	adj[9].push_back(5); adj[9].push_back(8);
    }
    int calc(string op, string ed)
    {
    	int r = 0;
    	for(int i = 0; i < 10; i++)
    	{
    		if((op[i] == '*' || ed[i] == '*') && op[i] != ed[i]) return INF;
    		if(op[i] != ed[i]) r++;
    	}
    	
    	//cout << op << endl << ed << endl;
    	//cout << r << endl;
    	return r;
    }
    list<int> adj[10];
    map<string, int> stat;
    queue<string> q;
    string swap(string a, int op, int ed)
    {
    	string b = a;
    	char tmp = b[op];
    	b[op] = b[ed];
    	b[ed] = tmp;
    	
    	return b;
    }
};

int NinePuzzle::getMinimumCost(string init, string goal)
{
    initial();
    q.push(init);
    int ans = INF;
    int cg;
    int star;
    string tmp;
    string n;
    
    stat[init] = 1;
    while(!q.empty())
    {
    	tmp = q.front();
    	q.pop();
    	cg = calc(tmp, goal);
    	if(cg < ans) ans = cg;
    	if(ans == 0) break;
    	for(int i = 0; i < 10; i++)
    		if(tmp[i] == '*')
    		{
    			star = i;
    			break;
    		}
    	
    	for(list<int>::iterator it = adj[star].begin(); it != adj[star].end(); it++)
    	{
    		n = swap(tmp, star, *it);
    		if(1 != stat[n])
    		{
    			stat[n] = 1;
    			q.push(n);
    		}
    	}
    }
    
    return ans;
}

// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
bool KawigiEdit_RunTest(int testNum, string p0, string p1, bool hasAnswer, int p2) {
	cout << "Test " << testNum << ": [" << "\"" << p0 << "\"" << "," << "\"" << p1 << "\"";
	cout << "]" << endl;
	NinePuzzle *obj;
	int answer;
	obj = new NinePuzzle();
	clock_t startTime = clock();
	answer = obj->getMinimumCost(p0, p1);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "\t" << p2 << endl;
	}
	cout << "Your answer:" << endl;
	cout << "\t" << answer << endl;
	if (hasAnswer) {
		res = answer == p2;
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
int main() {
	bool all_right;
	all_right = true;
	
	string p0;
	string p1;
	int p2;
	
	{
	// ----- test 0 -----
	p0 = "BG*YRGRRYR";
	p1 = "BGGY*YRRRR";
	p2 = 0;
	all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
	// ------------------
	}
	
	{
	// ----- test 1 -----
	p0 = "GBBB*BGBBG";
	p1 = "RYYY*YRYYR";
	p2 = 9;
	all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
	// ------------------
	}
	
	{
	// ----- test 2 -----
	p0 = "RRBR*BRBBB";
	p1 = "BBRB*RBRRR";
	p2 = 1;
	all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
	// ------------------
	}
	
	if (all_right) {
		cout << "You're a stud (at least on the example cases)!" << endl;
	} else {
		cout << "Some of the test cases had errors." << endl;
	}
	return 0;
}
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
